import axios from 'axios';

const api = axios.create({
  baseURL: 'http://localhost:3003/api'
});

// 添加请求拦截器
api.interceptors.request.use(
  (config) => {
    // console.log('发送请求:', {
    //   url: config.url,
    //   method: config.method,
    //   headers: config.headers
    // });
    const token = localStorage.getItem('token');
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
    return config;
  },
  (error) => {
    // console.error('请求拦截器错误:', error);
    return Promise.reject(error);
  }
);

// 添加响应拦截器
api.interceptors.response.use(
  (response) => {
    // console.log('收到响应:', {
    //   status: response.status,
    //   data: response.data
    // });
    return response;
  },
  (error) => {
    if (error.response && error.response.status === 401) {
      // console.log('收到401响应，清除认证信息');
      localStorage.removeItem('token');
      localStorage.removeItem('user');
      // window.location.href = '/login';
    }
    return Promise.reject(error);
  }
);

export default api; 